<template>
  <!-- 分配班级弹窗 -->
  <el-dialog
    width="50%"
    title="分配班级"
    :visible.sync="openDlg"
    append-to-body="true"
    destroy-on-close
  >
    <!-- 添加或修改角色配置对话框 -->
    <el-form ref="form" :model="form" :rules="rules" label-width="95px">
     
      <selectCounselors
            ref="selectUserRef"
            :sendUserIds.sync="sendUserIds" :cId ='cId' :tId = 'tId'
          ></selectCounselors>
      <h2>班级选择</h2>

      <el-badge
        :value="sendUserIds.length"
        :hidden="!sendUserIds.length"
        :type="'primary'"
      >
        <el-button size="small" @click="selectUserBtn">选择</el-button>
      </el-badge>
    </el-form>
    <!-- <fromIndex :distributionRow="distributionRow"></fromIndex> -->
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit">确定</el-button>
      <el-button @click="openDlg = false">取消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { addApprclrinfo } from "@/api/clr/apprclrinfo";
import { listRole } from "@/api/system/role";

import { getUser, deptTreeSelect } from "@/api/system/user";
import { counsellorInforTid } from "@/api/counselor/index";
import selectCounselors from "./selectCounselors.vue";
import Validators from "@/utils/validate.js";
import { getProvinceTree } from "@/api/information/classInfor";
import { classTree } from "@/api/information/studentInfor";
export default {
  name: "Apprclrinfo",
  components: {
    selectCounselors,
  },

  dicts: [
    "clr_appr_status",
    "basic_education_type", // 学历类型
    "basic_degree_type", // 学位类型
    "sys_user_sex", // 性别
    "bc_political_type", // 政治面貌
    "bc_nation_type", // 民族类型
    "clr_counsellor_type", // 是否辅导员
    "clr_cthr_type", // 是否班主任
    "bc_office_type", // 任职类型
    "basic_post_status", //在岗状态
    "clr_learning_style", //学习方式
    "clr_learning_forms", //学习形式
    "clr_school_type", //学校类型
    "sys_yes_no", //学位类型
    "basic_blood_type", // 血型
    "basic_title_type", // 职称类型
    "basic_title_level", // 职称级别
    "clr_country_type", // 授予国家
    "bc_speciality_type", //学制类型
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedClrApprRecord: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      openDlg: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 辅导员信息申请单表格数据
      apprclrinfoList: [],
      // 辅导员审批记录表格数据
      clrApprRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      deptOptions: [],
      options: [], // 籍贯
      clrEducationList: [], //
      formWork: {},
      clrWorkHistoryList: [],
      educationSname: [],
      dataTime: [],
      workDisabled: false,
      educationDisabled: false,
      multipleEducation: true,
      sendUserIds: [],
      roleOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applyTno: null,
        applyName: null,
        applyTime: null,
        applyRule: null,
        apprStatus: null,
      },
      // 表单参数
      form: {
        teacher: {},
        deptId: "",
      },
      cId: "", //教师ID
      tId: '',
      type: null, // 调整类型（A:新增信息,E:修改信息）
      educationFrom: {},
      // 表单校验
      rules: {
        // deptId: [
        //   { required: true, message: "请选择所属院系", trigger: "change" }
        // ],
        admissionTime: [
          { required: true, message: "请选择来校时间", trigger: "change" },
        ],
        postStatus: [
          { required: true, message: "请选择岗位状态", trigger: "change" },
        ],
        tno: [
          {
            required: true,
            text: "请输入工号",
            validator: Validators.istext,
            trigger: "blur",
          },
        ],
        name: [
          {
            required: true,
            text: "请输入姓名",
            validator: Validators.isString,
            trigger: "blur",
          },
        ],
        sex: [{ required: true, message: "请选择性别", trigger: "change" }],
        bloodType: [
          { required: false, message: "请选择血型", trigger: "change" },
        ],
        cardCode: [
          { required: false, message: "请输入身份证号", trigger: "blur" },
          {
            message: "请输入正确的身份证号",
            pattern:
              /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]+$/,
            trigger: "blur",
          },
        ],
        phone: [
          { required: false, message: "请输入联系方式", trigger: "blur" },
          {
            message: "请输入正确的身份证号",
            pattern: /^1\d{10}$/,
            trigger: "blur",
          },
        ],
        birthday: [
          { required: false, message: "请选择出生日期", trigger: "change" },
        ],
        nation: [{ required: false, message: "请选择民族", trigger: "change" }],
        political: [
          { required: false, message: "请选择政治面貌", trigger: "change" },
        ],
        formerName: [
          {
            required: false,
            text: "请输入曾用名",
            // validator: Validators.isString,
            trigger: "blur",
          },
        ],
        bnpId: [
          { required: false, message: "请选择籍贯", trigger: "change" },
        ],
        education: [
          { required: false, message: "请选择学历", trigger: "change" },
        ],
        acDegree: [
          { required: false, message: "请选择学位", trigger: "change" },
        ],
        braidType: [
          { required: false, message: "请选择编制类型", trigger: "change" },
        ],
        title: [{ required: false, message: "请选择职称", trigger: "change" }],
        titleLevel: [
          { required: false, message: "请选择职称级别", trigger: "change" },
        ],
        nature: [
          { required: false, message: "请选择教师性质", trigger: "change" },
        ],
        roles: [{ required: true, message: "请选择角色", trigger: "change" }],
      },
    };
  },
  mounted() {
    this.department();
    // getUser().then((response) => {
    //   this.roleOptions = response.roles.filter((item) => {
    //     return item.roleName == "辅导员";
    //   });
    // });
    // 籍贯下拉书
    getProvinceTree().then((response) => {
      this.options = response.data;
    });
  },
  computed: {
    userInfor() {
      return this.$store.state.user.userInfor;
    },
  },
  methods: {
    openIndex(row) {
      console.log(row, "....");
      this.form = row.teacher;
      if (this.form.id) {
        // 查询辅导员信息列表
        counsellorInforTid(this.form.id).then((res) => {
          if (res.data) {
            this.cId = res.data.cId;
            this.tId = res.data.tId;
            this.clrEducationList = res.data.clrEducationList;
            this.clrWorkHistoryList = res.data.clrWorkHistoryList;
            this.sendUserIds = res.data.classList;
            console.log(res, "lllll");
            this.form = res.data.teacher;
          }else {
            getUser().then((response) => {
      this.roleOptions = response.roles.filter((item) => {
        return item.roleName == "辅导员";
      });
    });
          }
          console.log(this.cId, "dsadhskaj");
        });
      }
      this.openDlg = true;
    },
    // 提交
    submit() {
    let data = {
   ...this.form,
   cId: this.cId,
   tId: this.tId,
   classList:this.sendUserIds 
};
      this.$emit('getDate',data)
      this.openDlg = false
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    // 清除表单
    CloseIn() {
      this.form = {};
      this.formTeacher = {};
      this.open = false;
    },

    handleChange(value) {
      if (value) {
        this.form.deptId = value;
      }
      // this.form.deptId = value[2];
      // this.form.multistage = value + "";
    },
    // 根据身份证号获取出生年月
    codeBulr() {
      if (this.form.cardCode) {
        this.$set(this.form, "birthday", this.getBirthday(this.form.cardCode));
      }
    },
    // 弹窗取消
    educationalCancel() {
      this.educationalVisible = false;
      this.resetForm("educationFrom");
    },
    // 获取部门信息
    async department() {
      await classTree().then((res) => {
        this.deptOptions = res.data;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.caiId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 添加工作经历
    handleAddClrWorkHistory() {
      this.innerVisible = true;
      this.workDisabled = false;
      this.formWork = {};
    },
    handlePlace(data) {
      this.form.bnpId = data[data.length - 1];
    },
    // 添加教育背景
    educationalAdd() {
      this.educationalVisible = true;
      this.educationDisabled = false;
      this.educationFrom = {};
    },
    // 起止时间
    dateTimes() {
      console.log(this.dataTime, "dateTime");
      this.formWork.startDate = this.dataTime[0];
      this.formWork.endDate = this.dataTime[1];
    },
    // 工作经历
    submitWork() {
      console.log(this.dataTime, "submitWork");
      if (this.dataTime.length == 0) {
        this.$modal.msgWarning("请选择时间");
      } else {
        if (this.clrWorkHistoryList.length > 0) {
          this.clrWorkHistoryList.forEach((element) => {
            if (element.cName == this.formWork.cName) {
              this.$modal.msgWarning("添加的工作单位已存在");
            } else {
              this.$refs["formWork"].validate((valid) => {
                if (valid) {
                  this.clrWorkHistoryList = this.clrWorkHistoryList;
                  this.clrWorkHistoryList.push(this.formWork);
                  this.dataTime = [];
                  this.formWork = {};
                  this.innerVisible = false;
                }
              });
            }
          });
        } else {
          this.$refs["formWork"].validate((valid) => {
            if (valid) {
              this.clrWorkHistoryList = this.clrWorkHistoryList;
              this.clrWorkHistoryList.push(this.formWork);
              this.dataTime = [];
              this.formWork = {};
              this.innerVisible = false;
            }
          });
        }
      }
    },
    selectUserBtn() {
      this.$refs.selectUserRef.open();
    },
    // 工作经历多选
    handleClrWorkHistorySelectionChange(selection) {
      this.workCname = selection;
      this.multiple = !selection.length;
    },
    // 教育背景多选
    handleClrEducationSelectionChange(selection) {
      this.educationSname = selection;
      this.multipleEducation = !selection.length;
    },
    // 教育背景参数添加
    submitEducation() {
      if (this.clrEducationList.length > 0) {
        this.clrEducationList.forEach((element) => {
          if (element.sName == this.educationFrom.sName) {
            this.$modal.msgWarning("添加的学校已存在");
          } else if (
            (element.isDegree == "Y" && this.educationFrom.isDegree == "Y") ||
            (element.isEducation == "Y" &&
              this.educationFrom.isEducation == "Y")
          ) {
            this.$modal.msgWarning("最高学历或最高学位已存在");
          } else {
            this.$refs["educationFrom"].validate((valid) => {
              if (valid) {
                this.clrEducationList = this.clrEducationList;
                this.clrEducationList.push(this.educationFrom);
                this.educationalVisible = false;
                this.educationFrom = {};
              } else {
              }
            });
          }
        });
      } else {
        this.$refs["educationFrom"].validate((valid) => {
          if (valid) {
            this.clrEducationList = this.clrEducationList;
            console.log(this.educationFrom, "push");
            this.clrEducationList.push(this.educationFrom);
            console.log(this.clrEducationList, "添加");

            this.educationalVisible = false;
            this.educationFrom = {};
          } else {
          }
        });
      }
    },

    // 删除教育背景
    handleDeleteClrEducationHistory(index, rows) {
      if (rows) {
        rows.splice(index, 1);
      } else {
        this.clrEducationList = this.educationSname.filter(
          (item) => !this.educationSname.some((it) => it.sName === item.sName)
        );
      }
    },
    // 教育背景查看详情
    educationDetails(row) {
      this.educationFrom = row;

      this.educationalVisible = true;
      this.educationDisabled = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.sendUserIds = this.sendUserIds.map((i) => {
        return {
          classId: i.id,
          ...i,
        };
      });
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let data = {
            teacher: {},
            clrWorkHistoryList: [],
            clrEducationList: [],
            classList: this.sendUserIds,
          };
          data.teacher = this.form;

          data.clrWorkHistoryList = this.clrWorkHistoryList;
          data.clrEducationList = this.clrEducationList;
          if (this.cId == "" || this.cId == null) {
            this.type = "A";
            data.tId = this.form.id;
          } else {
            this.type = "E";
            data.cId = this.cId;
            data.tId = this.form.id;
          }
          addApprclrinfo({
            applyRule: this.type,
            parameter: JSON.stringify(data),
          }).then((response) => {
            this.$modal.msgSuccess("信息提交成功");
            this.$router.push({ path: "/counselor/clrApply/apprclrinfo" });
          });
        }
      });
    },
    /** 辅导员审批记录序号 */
    rowClrApprRecordIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 辅导员审批记录添加按钮操作 */
    handleAddClrApprRecord() {
      let obj = {};
      obj.confSeq = "";
      obj.apprTno = "";
      obj.apprName = "";
      obj.apprRole = "";
      obj.apprTime = "";
      obj.apprStatus = "";
      this.clrApprRecordList.push(obj);
    },
    /** 辅导员审批记录删除按钮操作 */
    handleDeleteClrApprRecord() {
      if (this.checkedClrApprRecord.length == 0) {
        this.$modal.msgError("请先选择要删除的辅导员审批记录数据");
      } else {
        const clrApprRecordList = this.clrApprRecordList;
        const checkedClrApprRecord = this.checkedClrApprRecord;
        this.clrApprRecordList = clrApprRecordList.filter(function (item) {
          return checkedClrApprRecord.indexOf(item.index) == -1;
        });
      }
    },
    /** 复选框选中数据 */
    handleClrApprRecordSelectionChange(selection) {
      this.checkedClrApprRecord = selection.map((item) => item.index);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "clr/apprclrinfo/export",
        {
          ...this.queryParams,
        },
        `apprclrinfo_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
      